package com.lisn.mylib.ui.home;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleEventObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.lisn.commonlib.LiveDataBus;
import com.lisn.mylib.R;
import com.lisn.mylib.ui.activity.LiveDataBusTestActivity;
import com.lisn.navannotationlib.FragmentDestination;
import com.lisn.sgdouyin.SGDouYinTestActivity;
import com.nari.horizontalselectviewlib.MyHSVActivity;
import com.nari.horizontalselectviewlib.autoLocateHorizontal.test.MyALHActivity;


@FragmentDestination(pageUrl = "main/tabs/home", asStarter = true)
public class HomeFragment extends Fragment {

    private static final String TAG = HomeFragment.class.getSimpleName();

    private HomeViewModel homeViewModel;
    private LifecycleOwner viewLifecycleOwner;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {

        Log.e(TAG, "onCreateView: ");

        homeViewModel = new ViewModelProvider(this).get(HomeViewModel.class);
        View root = inflater.inflate(R.layout.fragment_home, container, false);
        final TextView textView = root.findViewById(R.id.text_home);
        viewLifecycleOwner = getViewLifecycleOwner();
        homeViewModel.getText().observe(viewLifecycleOwner, new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                textView.setText(s);
            }
        });

        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e(TAG, "onClick: ");
//                RecyclerViewTestActivity.startActivity(getActivity());
//                ImgVideoInfoActivity.startActivity(getActivity());
//                MyHSVActivity.startActivity(getActivity());
//                MyALHActivity.startActivity(getActivity());
//                BarActivity.startActivity(getActivity());
//                RoomTestActivity.startActivity(getActivity());
//                LiveDataBusTestActivity.startActivity(getActivity());

//                TestOpenGLActivity.startActivity(getActivity());
                SGDouYinTestActivity.startActivity(getActivity());

            }
        });

        LifecycleObserver();
        return root;
    }

    private void LifecycleObserver() {
        viewLifecycleOwner.getLifecycle().addObserver(new LifecycleEventObserver() {
            @Override
            public void onStateChanged(@NonNull LifecycleOwner source, @NonNull Lifecycle.Event event) {
                Log.e(TAG, "onStateChanged: " + event.toString());
            }
        });

        LiveDataBus.get().with(LiveDataBusTestActivity.EVENT_POST_STICKY_DATA)
                .observe(viewLifecycleOwner, new Observer() {
                    @Override
                    public void onChanged(Object o) {
                        Log.e(TAG, "onChanged: " + o.toString());
                    }
                });

        LiveDataBus.get().with(LiveDataBusTestActivity.EVENT_POST_VALUE)
                .observe(viewLifecycleOwner, new Observer() {
                    @Override
                    public void onChanged(Object o) {
                        Log.e(TAG, "onChanged: " + o.toString());
                    }
                });
    }


    //region Description


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e(TAG, "onCreate: ");
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e(TAG, "onDestroy: ");
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Log.e(TAG, "onViewCreated: ");
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.e(TAG, "onDestroyView: ");
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.e(TAG, "onActivityCreated: ");
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        Log.e(TAG, "onAttach: ");
    }

    @Override
    public void onDetach() {
        super.onDetach();
        Log.e(TAG, "onDetach: ");
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.e(TAG, "onStart: ");
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.e(TAG, "onResume: ");
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.e(TAG, "onPause: ");
    }

    @Override
    public void onStop() {
        super.onStop();
        Log.e(TAG, "onStop: ");
    }
    //endregion

}